﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.Core.DomainModel;

namespace MethodWorx.CMS.Plugins.AssetManagement
{
    [Serializable]
    public class AssetFolder : AggregateRoot
    {
        public AssetFolder [] Children { get; set; }
        public string Name { get; set; }
        public bool TemplateAssets { get; set; }

        public AssetFolder()
        {
            this.Children = new AssetFolder[0];
        }

        public AssetFolder CreateAssetFolder(string name, bool templateAssets)
        {
            var list = new List<AssetFolder>(this.Children);
            var assetFolder = new AssetFolder()
                                  {
                                      Name = name,
                                      TemplateAssets = templateAssets,
                                      Id = Guid.NewGuid()
                                  };
            list.Add(assetFolder);

            this.Children = list.ToArray();

            return assetFolder;
        }

        public AssetFolder Find(Guid? objectId)
        {
            if (!objectId.HasValue)
                objectId = Guid.Empty;

            if (this.Id == objectId)
                return this;
            else
            {
                if (this.Children != null)
                {
                    foreach (AssetFolder child in this.Children)
                    {
                        var found = child.Find(objectId);
                        if (found != null)
                            return found;
                    }
                }

                return null;
            }
        }

        public AssetFolder FindWithParent(Guid? objectId, out AssetFolder parent)
        {
            parent = null;
            if (!objectId.HasValue)
                objectId = Guid.Empty;

            if (this.Id == objectId)
                return this;
            else
            {
                if (this.Children != null)
                {
                    foreach (AssetFolder child in this.Children)
                    {
                        var found = child.FindWithParent(objectId, out parent);
                        if (found != null)
                        {
                            if(parent  == null)
                                parent = this;
                            return found;
                        }
                    }
                }

                return null;
            }
        }

        internal IEnumerable<AssetFolder> GetChildren(Guid? objectId)
        {
            if (!objectId.HasValue)
                objectId = Guid.Empty;

            if (this.Id == objectId)
                return this.Children;
            else
            {
                foreach (AssetFolder child in this.Children)
                {
                    var children = child.GetChildren(objectId.Value);
                    if (children != null)
                        return children;
                }

                return null;
            }
        }



       
    }
}
